-
Notifications
You must be signed in to change notification settings - Fork 92
gppa-sort-by-time-field.php: Added new snippet.
#1097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughA new PHP snippet has been added to enable chronological sorting of time values when populating a Gravity Forms choice field from entries. The code hooks into a specific Gravity Forms filter, sorts the choices by converting their text to Unix timestamps, and returns the sorted array for display. Changes
Sequence Diagram(s)sequenceDiagram
participant GravityForms
participant CustomSnippet
GravityForms->>CustomSnippet: Apply gppa_input_choices_{form_id}_{field_id} filter with choices array
CustomSnippet->>CustomSnippet: Sort choices by converting text to Unix timestamps
CustomSnippet-->>GravityForms: Return sorted choices array
GravityForms->>User: Display sorted choices in the form field
Suggested reviewers
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (3)
gp-populate-anything/gppa-sort-by-time-field.php (3)
2-12: Enhance documentation and plugin header for standalone use.
The current DocBlock covers usage, but if users install this as a self-contained plugin, consider adding standard WordPress plugin header metadata (Plugin Name, Description, Version, Author) at the top. You can also annotate the filter callback parameters for clarity.
13-14: Clarify hook customization instructions.
Having users manually replace123and4in the filter name works, but you could extract them into clearly named constants or variables at the top. This improves readability and makes future edits less error-prone.
24-24: Add final newline.
Ensure the file ends with a single trailing newline to comply with POSIX standards and prevent potential concatenation issues.
| usort( $choices, function( $a, $b ) { | ||
| $timeA = strtotime( $a['text'] ); | ||
| $timeB = strtotime( $b['text'] ); | ||
| return $timeA - $timeB; | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Handle invalid time formats and strengthen comparator.
strtotime() returns false on unparseable strings, which casts to 0 and may scramble ordering. Also, returning raw subtraction can overflow on large timestamps. Consider validating timestamps and returning -1/0/1 explicitly. For example:
usort( $choices, function( $a, $b ) {
$timeA = strtotime( $a['text'] );
$timeB = strtotime( $b['text'] );
if ( $timeA === false || $timeB === false ) {
// Place unparsable entries at the end
return $timeA === false && $timeB !== false
? 1
: ( $timeA !== false && $timeB === false ? -1 : 0 );
}
return $timeA < $timeB ? -1 : ( $timeA > $timeB ? 1 : 0 );
});
saifsultanc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2908841996/82222?viewId=7627047
💬 Slack: https://gravitywiz.slack.com/archives/GMP0ZMNSE/p1746536433476309
Summary
This snippet is intended for when populating Times from a GF entry into a choice-based field. It will sort the times chronologically.